package me.winds.infrared;

import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.easyder.wrapper.utils.PreferenceUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * Author:  winds
 * Data:    2018/5/28
 * Version: 1.0
 * Desc:
 */
public class Helper {

    public static final String BASIC_USER = "user"; //用户信息
    public static final String BASIC_TOKEN = "token";
    public static final String BASIC_ACCOUNT = "account";
    public static final String BASIC_SEARCH_RECORD = "record";


    public static void setAccount(String account) {
        PreferenceUtils.putPreference(WrapperApplication.getInstance(), BASIC_ACCOUNT, account);
    }

    public static String getAccount() {
        return PreferenceUtils.getPreference(WrapperApplication.getInstance(), BASIC_ACCOUNT, null);
    }


/*    public final static void saveAccountInfo(AccountVo info) {
        try {
            if (info != null) {
                String result = encode(JSONObject.toJSONString(info));
                PreferenceUtils.putPreference(WrapperApplication.getInstance(), BASIC_USER, result);
            } else {

                PreferenceUtils.putPreference(WrapperApplication.getInstance(), BASIC_USER, null);
            }
        } catch (Exception e) {

        }
    }*/

/*    public static AccountVo getAccountInfo() {
        String result = PreferenceUtils.getPreference(WrapperApplication.getInstance(), BASIC_USER, null);
        if (!TextUtils.isEmpty(result)) {
            return JSONObject.parseObject(decode(result), AccountVo.class);
        }
        return null;
    }*/


    public static void setSearchRecord(List<String> list) {
        if (list != null) {
            String result = encode(JSONObject.toJSONString(list));
            PreferenceUtils.putPreference(WrapperApplication.getInstance(), BASIC_SEARCH_RECORD, result);
        } else {
            PreferenceUtils.putPreference(WrapperApplication.getInstance(), BASIC_SEARCH_RECORD, null);
        }
    }

    public static List<String> getSearchRecord() {
        String s = decode(PreferenceUtils.getPreference(WrapperApplication.getInstance(), BASIC_SEARCH_RECORD, null));
        if (TextUtils.isEmpty(s)) {
            return new ArrayList<>();
        } else {
            return JSON.parseObject(s, new TypeReference<ArrayList<String>>() {
            });
        }
    }

    public static void setToken(String token) {
        PreferenceUtils.putPreference(WrapperApplication.getInstance(), BASIC_TOKEN, token);
    }

    public static String getToken() {
        return PreferenceUtils.getPreference(WrapperApplication.getInstance(), BASIC_TOKEN, null);
    }

    /**
     * 编码字符串
     *
     * @param data
     * @return
     */
    public static String encode(String data) {
//        return data == null ? null : Base64.encode(data);
        return "";
    }

    /**
     * 解码字符串
     *
     * @param result
     * @return
     */
    public static String decode(String result) {
//        return result == null ? null : Base64.decode(result);
        return "";
    }
}
